package in.hitmedia.test.stack;

public class Stack<E>
{
	public Node<E> head=null;
	
	public void push(final E data)
	{
		Node<E> n = new Node<E>(data, head);
		head = n;
	}
	
	public Node<E> pop()
	{
		Node<E> toReturn = head;
		head=head.getNext();
		
		return toReturn;
	}
	
	public E peek()
	{
		if (head != null)
		{
			return head.getData();
		}
		else
		{
			return null;
		}
	}
	
	@Override
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		
		Node<E> n = head;
		sb.append("[ ");
		while (n != null)
		{
			sb.append(n.getData()).append(" ");
			n = n.getNext();
		}
		
		sb.append("]");
		
		return sb.toString();
	}
	

}
